﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iTunesLib;
using System.Net;
using System.Text.RegularExpressions;
using System.Xml;
using System.Web;
using Microsoft.VisualBasic.CompilerServices;



namespace iLyrics
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!lyricsThread.IsBusy)
            {
                UpdateTrackListing();
                if (dataGridView1.Rows.Count > 0)
                {
                    progressBar.Maximum = dataGridView1.Rows.Count;
                    lyricsThread.RunWorkerAsync();
                    startToolStripMenuItem.Enabled = false;
                    stopToolStripMenuItem.Enabled = true;
                    statusImage.Image = iLyrics.Properties.Resources.Play_Hot_24x24;
                }
            }
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lyricsThread.IsBusy)
            {
                lyricsThread.CancelAsync();
            }
        }

        private void lyricsThread_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            List<IITTrack> tracks = dataGridView1.DataSource as List<IITTrack>;
            LyricFinder lyricHelper = new LyricFinder(LyricFinder.Options.All);

            for (int i = 0; i < tracks.Count; i++)
            {
                string lyrics = lyricHelper.getLyrics(tracks[i].Artist, tracks[i].Name);

                if (lyrics != null)
                {
                    ((IITFileOrCDTrack)tracks[i]).Lyrics = lyrics;
                    dataGridView1.Rows[i].Cells["Lyrics"].Value = true;
                }
                else
                {
                    dataGridView1.Rows[i].Cells["Lyrics"].Value = false;
                }
                worker.ReportProgress(i + 1);
                if (worker.CancellationPending)
                    return;
            }

        }
        private void lyricsThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar.Value = e.ProgressPercentage;
            statusCount.Text = e.ProgressPercentage + " / " + dataGridView1.Rows.Count;

        }

        private void lyricsThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            startToolStripMenuItem.Enabled = true;
            stopToolStripMenuItem.Enabled = false;
            statusImage.Image = iLyrics.Properties.Resources.Stop_Normal_Red_24x24;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void UpdateTrackListing()
        {
            try
            {
                iTunesApp app = new iTunesApp();
                List<IITTrack> tracks = app.SelectedTracks.Cast<IITTrack>().ToList();
                dataGridView1.DataSource = tracks;
                statusCount.Text = "0 / " + tracks.Count;
            }
            catch
            {
                MessageBox.Show("Please select tracks that you wish to update");
            }
        }


        private void overwriteFoundLyricsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!overwriteAllToolStripMenuItem.Checked)
                overwriteFoundLyricsToolStripMenuItem.Checked = !overwriteFoundLyricsToolStripMenuItem.Checked;
        }

        private void overwriteAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            overwriteAllToolStripMenuItem.Checked = !overwriteAllToolStripMenuItem.Checked;
            overwriteFoundLyricsToolStripMenuItem.Checked = true;
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

    }
}
